package com.wwdx.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.wwdx.model.WwdxBookModel;
import com.wwdx.model.WwdxCommentModel;
import com.wwdx.model.WwdxThemeModel;
import com.wwdx.model.WwdxWeixinUserModel;
import com.wwdx.service.WwdxCommentServiceI;
import com.wwdx.utils.ConstantHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author liguoliang
 * @date 2018/5/23 14:45
 */
@Service
public class WwdxCommentServiceImpl extends BaseServiceImpl<WwdxCommentModel> implements WwdxCommentServiceI {

    @Override
    public Long selectModelCount(WwdxCommentModel model) {
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(4);
        String hql = "select count(1) from WwdxCommentModel\t"+getParams(model,params);
        return count(hql,params);
    }

    private String getParams(WwdxCommentModel model, Map<String, Object> params){
        StringBuilder sb = new StringBuilder("where 1=1\n");
        Integer type = model.getType();
        if (type != null){
            sb.append("and type=:type\t");
            params.put("type",type);
        }
        Long productId = model.getProductId();
        if (productId != null){
            params.put("productId",productId);
            sb.append("and productId=:productId\t");
        }
        Integer recommend = model.getRecommend();
        if (recommend != null){
            params.put("recommend",recommend);
            sb.append("and recommend=:recommend\t");
        }
        sb.append("and pid = 0");
        return sb.toString();
    }

    @Override
    public List<WwdxCommentModel> selectModelList(WwdxCommentModel model, Integer page, Integer rows) {
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(4);
        String hql = "from WwdxCommentModel\t"+getParams(model,params)+"\torder by gmtCreated desc";
        return this.execute(find(hql, params, page, rows),model.getRest(),model.getOpenId());
    }

    private List<WwdxCommentModel> execute(List<WwdxCommentModel> commentModels, boolean rest, String xopenId){
        commentModels.forEach(wwdxCommentModel->{
            if (rest){
                Long id = wwdxCommentModel.getId();
                List<WwdxCommentModel> replys = this.getReplys(id);
                if (!CollectionUtils.isEmpty(replys)){
                    wwdxCommentModel.setReplys(replys);
                }
                String likeKey = String.format(ConstantHolder.LIKE_USER_SET, id);
                Set<Object> smembers = setOperations.members(likeKey);
                if (!CollectionUtils.isEmpty(smembers)){
                    List<String> openIds = Lists.newArrayListWithExpectedSize(smembers.size());
                    smembers.forEach(s -> openIds.add(s.toString()));
                    wwdxCommentModel.setOpenIds(openIds);
                    wwdxCommentModel.setLikeCount((long)smembers.size());
                    wwdxCommentModel.setIslike(setOperations.isMember(likeKey, xopenId));
                }
            }else {
                Long productId = wwdxCommentModel.getProductId();
                WwdxThemeModel wwdxThemeModel = wwdxThemeService.getByThemeId(productId);
                if (wwdxThemeModel != null){
                    wwdxCommentModel.setProductName(wwdxThemeModel.getTitle());
                }
                WwdxBookModel wwdxBookModel = wwdxBookService.getByBookId(productId);
                if (wwdxBookModel != null){
                    wwdxCommentModel.setProductName(wwdxBookModel.getBookname());
                }
            }
            WwdxWeixinUserModel wwdxWeixinUserModel = wwdxWeixinUserService.getByOpenId(wwdxCommentModel.getOpenId());
            wwdxCommentModel.setNickname(wwdxWeixinUserModel.getNickname());
            wwdxCommentModel.setHeadimg(wwdxWeixinUserModel.getHeadImgUrl());
        });
        return commentModels;
    }

    @Override
    public List<WwdxCommentModel> getReplys(Long id) {
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(2);
        params.put("id",id);
        String hql = "from WwdxCommentModel where pid=:id";
        return find(hql,params);
    }
}